The present invention relates generally to runtime linking and unlinking, and more particularly to a system and method for unlinking a branch linking code fragments in a caching dynamic translator during runtime.
Caching dynamic translators use a code cache to store native optimized translations of frequently executed code fragments, which reduces emulation overhead and improves performance. When a branch instruction is reached in a fragment in the code cache, processing exits from the code cache. In instances where the branch instruction branches to another fragment in the code cache, there is a context switch from processing in the code cache to outside the code cache and back into the cache. These context switches in and out of the code cache are expensive.
To avoid these expensive context switches, it is possible to link or xe2x80x9cbackpatchxe2x80x9d branches that exit the cache only to jump right back into another code fragment that is already in the cache. Linking minimizes the expensive context switches necessary for implementing the cache exits and entries. Linking fragments in the code cache also results in some problems.
The contents of the code cache typically change with the working set of the application program. It is therefore desirable to be able to remove older fragments as newer ones enter the code cache. Any fragment being removed from the code cache, which has been previously linked to another fragment in the code cache, needs to be unlinked. Branch unlinking adds to the overhead of dynamic translation. If unlinking is too expensive to be feasible at runtime, the system has to compensate for the inability to quickly remove fragments by enlarging the size of the code cache, which becomes undesirable after a certain point.
Briefly, in a dynamic translator in which code fragments are stored in a cache, a method for linking and unlinking a first code fragment stored and a second code fragment in the cache comprises associating a memory area with a branch in a first code fragment that branches outside of the cache, and storing at least one instruction in the memory area that is executed when the branch is taken and control is to transfer to code outside of the cache. If it is determined that the branch can be set to branch to a location in a second code fragment stored in the cache, information is stored in the associated memory area from which the branch can be reconstructed in response to the determination, and the branch is changed so that it branches to the second code fragment stored in the code cache, thereby linking the first and second code fragments.
In another aspect of the present invention, if it is determined that the branch from the first code fragment to the second code fragment should be unlinked, the branch is reconstructed to its state before linking based on the information stored in the associated memory area.
In yet another aspect of the present invention, a method for linking and unlinking code fragments stored in a code cache comprises associating a memory area with a branch in a first code fragment that branches outside the cache. If it is determined that the branch can be set to branch to a location in a second code fragment stored in the cache, branch reconstruction information is stored in the memory area associated with the branch, and the branch instruction is updated to branch to the determined location in the second code fragment, thereby linking the first code fragment to the second code fragment.